import time

from playwright.sync_api import sync_playwright, ViewportSize
from logging import log


class command:

    def __init__(self, logging, page):
        """

        :param logging:
        :param page: sync_playwright.sync_api.Page
        """
        self.logging = logging
        self.page = page
        assert isinstance(self.page,sync_playwright.sync_api.Page )

    def __waitForView(self):
        time.sleep(1)

    def reset(self):
        self.page.goto("http://localhost:3000/dashboard/analysis")

    def goToIssueCmdList(self):
        """
        跳转到下达命令列表

        :return:
        """

        page = self.page
        # Click text=系统管理
        page.click("text=系统管理")
        # Click text=下达动员决心
        page.click("text=下达动员决心")
        # Click a:has-text("下达命令列表")
        page.click("a:has-text(\"下达命令列表\")")
        # assert page.url == "http://localhost:3000/deploy/isssueCmdV1.0.1/DeployIssuedCmdsList"

    def login(self):
        self.logging.info("start logging")
        # topic start:  设置窗口大小
        self.page.set_viewport_size({"width": 1888, "height": 900})
        # topic end

        # Go to http://localhost:3000/user/login
        self.page.goto("http://localhost:3000/user/login")

        # Click [placeholder="请输入帐户名 / admin"]
        self.page.click("[placeholder=\"请输入帐户名 / admin\"]")

        # Fill [placeholder="请输入帐户名 / admin"]
        self.page.fill("[placeholder=\"请输入帐户名 / admin\"]", "admin")

        # Press Tab
        self.page.press("[placeholder=\"请输入帐户名 / admin\"]", "Tab")

        # Fill [placeholder="密码 / 123456"]
        self.page.fill("[placeholder=\"密码 / 123456\"]", "123456")

        # Click [placeholder="请输入验证码"]
        self.page.click("[placeholder=\"请输入验证码\"]")

        # Fill [placeholder="请输入验证码"]
        self.page.fill("[placeholder=\"请输入验证码\"]", "6666")

        # Click button:has-text("确 定")
        self.page.click("button:has-text(\"确 定\")")
        # time.sleep(20)
        # 欢迎进入贵阳市智能动员大数据信息管理系统

        # Click text=欢迎进入贵阳市智能动员大数据信息管理系统
        self.page.click("text=欢迎进入贵阳市智能动员大数据信息管理系统")
        self.logging.info("logging success")
        # ---------------------
        # context.close()
        return self

    def testIssueCommand(self):
        """
        测试下达动员命令列表
        :return:
        """
        self.reset()
        page = self.page
        self.goToIssueCmdList()

        # 查看详情
        self.__waitForView()
        # Click text=查看导出发送短信查看导出查看导出查看导出下达命令查看导出查看导出查看导出查看导出查看导出查看导出 >> a
        page.click("text=查看导出发送短信查看导出查看导出查看导出下达命令查看导出查看导出查看导出查看导出查看导出查看导出 >> a")
        # assert page.url == "http://localhost:3000/deploy/isssueCmdV1.0.1/DetailIssueCmd?id=T03&code=T03"

        self.__waitForView()
        # 导出
        # Click a:has-text("下达命令列表")
        page.click("a:has-text(\"下达命令列表\")")
        # assert page.url == "http://localhost:3000/deploy/isssueCmdV1.0.1/DeployIssuedCmdsList"
        # Click .ant-table-body-inner .ant-table-fixed .ant-table-tbody .ant-table-row.ant-table-row-hover .ant-table-row-cell-break-word span span:nth-child(2) a

        self.__waitForView()
        # 发送短信
        # Click a:has-text("下达命令列表")
        page.click("a:has-text(\"下达命令列表\")")
        # Click :nth-match(:text("发送短信"), 2)
        page.click(":nth-match(:text(\"发送短信\"), 2)")

        self.__waitForView()

        # Click button:has-text("导出短信")
        # 去顶发送
        page.click("button:has-text(\"导出短信\")")
        self.__waitForView()

        # 取消发送
        # Click button:has-text("取消发送")
        page.click("button:has-text(\"取消发送\")")
        self.__waitForView()

        return self

    def cmdTabChange(self):
        """
        命令的 tab 该表
        :return:
        """
        page = self.page
        self.goToIssueCmdList()
        # Click text=生成中(14)
        page.click("text=生成中(14)")
        self.__waitForView()
        # Click text=待下达(1)
        page.click("text=待下达(1)")
        self.__waitForView()
        # Click text=执行中(5)
        page.click("text=执行中(5)")
        self.__waitForView()
        # Click text=已完成(3)
        page.click("text=已完成(3)")
        self.__waitForView()
        # Click text=已归档(1)
        page.click("text=已归档(1)")
        self.__waitForView()
        # Click text=已作废(1)
        page.click("text=已作废(1)")

        return self
